﻿using Autofac;
using Newtonsoft.Json;
using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.H5.Models;
using Pay.IService;
using Pay.Service;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Pay.H5.Controllers {
    [AppAuthorize("Member")]
    public class ShopCartController : GeneralController {
        public ShopCartController() { }
        private ICartService _cartService;
        private IProductService _productService;
        private IMerchantService _merchantService;
        private IShippingAddressService _shippingAddressService;
        private IPostFeeService _postFeeService;
        private IRegionService _regionService;
        private ISettingService _settingService;
        private IOrderService _orderService;
        private IPayHtmlService _iPayHtmlService;
        private IPayInfoService _payInfoService;
        private IGradeService _iGradeService;
        private IShippingAddressService _iShippingAddress;
        private IShopInfoService _shopInfoService;

        public ShopCartController(ICartService cartService, IPayInfoService payInfoService,
                                  IPayHtmlService payHtmlService, IProductService productService, IMerchantService merchantService,
                                  IShippingAddressService shippingAddressService, IPostFeeService postFeeService,
                                  IRegionService regionService, IGradeService iGradeService, IShopInfoService shopInfoService,
                                  ISettingService settingService, IOrderService orderService, IShippingAddressService iShippingService) {
            _payInfoService = payInfoService;
            _productService = productService;
            _merchantService = merchantService;
            _cartService = cartService;
            _shippingAddressService = shippingAddressService;
            _postFeeService = postFeeService;
            _regionService = regionService;
            _settingService = settingService;
            _orderService = orderService;
            _iPayHtmlService = payHtmlService;
            _iGradeService = iGradeService;
            _iShippingAddress = iShippingService;
            _shopInfoService = shopInfoService;

        }
        /// <summary>
        /// 新增购物车
        /// </summary>
        /// <returns>The car.</returns>
        /// <param name="product_id">Product identifier.</param>
        /// <param name="sku_id">Sku identifier.</param>
        /// <param name="quantity">Quantity.</param>
        public JsonResult AddCar(long product_id, long? sku_id, int quantity = 1) {
            if (product_id < 0)
                throw new ServerErrorException("商品不存在");
            if (quantity < 0)
                throw new ServerErrorException("商品数量异常");
            var model = new cart_dto {
                merchant_id = UserId,
                product_id = product_id,
                sku_id = sku_id,
                quantity = quantity
            };
            int result = _cartService.Add(model);
            if (result < 0) {
                api_result.result_msg = "操作失败";
                api_result.result_code = 500;
            }
            return Json(api_result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取购物车列表
        /// </summary>
        /// <returns>The cart list.</returns>
        public ActionResult ShopCartList() {
            CartQuery query = new CartQuery();
            query.merchant_id = UserId;
            var lists = _cartService.GetList(query);

            var dics = new List<cart_ex>();
            //var rows = lists.rows.GroupBy(c => new { c.product.merchant_id, c.product.merchant.name, c.product.merchant.entrepot.entrepot_name });
            var rows = lists.rows.GroupBy(c => new { c.product.merchant_id, c.product.merchant.name });
            foreach (var row in rows) {
                var carts = row.ToList();
                var cart_item = new cart_ex {
                    merchant_id = row.Key.merchant_id,
                    //entrepot_name = row.Key.entrepot_name,
                    shop_name = row.Key.name
                };
                cart_item.carts = carts;
                dics.Add(cart_item);
            }
            ViewData["list"] = dics;
            var likelist = new PagedResult<product_dto>();
            var goodsquery = new GoodsQuery();
            if (lists.rows != null && lists.rows.Count > 0) {
                var oem = _merchantService.Get(RequestMethodh5.MerchantNO);
                goodsquery.merchant_id = oem.id;
                goodsquery.audit_status = 1;
                goodsquery.sale_status = 0;
                goodsquery.sell_out = false;
                goodsquery.size = 4;
                goodsquery.sort = 1;
                likelist = _productService.Get(goodsquery);
            }
            ViewData["url"] = site.img_url;
            ViewData["likelist"] = likelist;
            return View();
        }
        /// <summary>
        /// 删除购物车
        /// </summary>
        /// <returns>The delete.</returns>
        /// <param name="Id">Identifier.</param>
        public JsonResult Delete(long id) {
            int result = _cartService.Delete(id);
            if (result < 0) {
                api_result.result_code = 500;
                api_result.result_msg = "操作失败";
            }
            return Json(api_result, JsonRequestBehavior.AllowGet);
        }
    }
}